package com.laiketui.admin.store.controller.indexData;

import com.alibaba.fastjson.JSONObject;
import com.laiketui.admin.store.api.IndexDubboService;
import com.laiketui.common.api.index.IndexDataService;
import com.laiketui.common.api.material.MaterialService;
import com.laiketui.common.mapper.ProductListModelMapper;
import com.laiketui.core.domain.Result;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.domain.product.ProductListModel;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.admin.DefaultViewVo;
import com.laiketui.domain.vo.index.IndexDataVo;
import com.laiketui.domain.vo.material.MaterialVo;
import com.laiketui.root.annotation.HttpApiMethod;
import io.seata.common.util.LambdaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 首页数据
 */
@Api(tags = "后台-商城后台小程序首页数据管理")
@RestController
@RequestMapping("/admin/index/indexData")
public class IndexDataController {

    @Autowired
    private IndexDataService indexDataService;

    @Autowired
    private IndexDubboService indexDubboService;

    @Autowired
    private ProductListModelMapper productListModelMapper;

    @Autowired
    private MaterialService materialService;

    @ApiOperation("查询首页数据")
    @RequestMapping("/selectInfo")
    @HttpApiMethod(apiKey = "admin.index.selectInfo")
    public Result selectInfo(IndexDataVo vo) {
        return Result.success(indexDataService.selectInfo(vo));
    }

    @ApiOperation("查询首页数据列表")
    @RequestMapping("/selectList")
    @HttpApiMethod(apiKey = "admin.index.selectList")
    public Result selectList(MainVo vo) {
        return Result.success(indexDataService.selectList(vo));
    }

    @ApiOperation("新增首页数据")
    @PostMapping("/insertIndexData")
    @HttpApiMethod(apiKey = "admin.index.insertIndexData")
    public Result insertIndexData(@Validated IndexDataVo vo) {
//        JSONObject object = JSONObject.parseObject(vo.getItems());
        try {
            indexDataService.insertIndexData(vo);
            return Result.success();
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("修改首页数据")
    @RequestMapping("/updateIndexData")
    @HttpApiMethod(apiKey = "admin.index.updateIndexData")
    public Result updateIndexData(@Validated IndexDataVo vo) {
        try {
            indexDataService.updateIndexData(vo);
            return Result.success();
        } catch (LaiKeAPIException e) {
            System.out.println(e.getCode() + ":" + e.getMessage());
            return Result.error(e.getCode(), e.getMessage());
        }

    }

    @ApiOperation("删除首页数据")
    @RequestMapping("/deleteIndexData")
    @HttpApiMethod(apiKey = "admin.index.deleteIndexData")
    public Result deleteIndexData(IndexDataVo vo) {
        return Result.success(indexDataService.deleteIndexData(vo));
    }

    @ApiOperation("首页数据上下架")
    @RequestMapping("/updateStatus")
    @HttpApiMethod(apiKey = "admin.index.updateStatus")
    public Result updateStatus(IndexDataVo vo) {
        return Result.success(indexDataService.updateStatus(vo));
    }

    @ApiOperation("后台首页数据商品列表")
    @PostMapping("/productList")
    @HttpApiMethod(apiKey = "admin.index.productList")
    public Result productList(MainVo vo, String keyword) {
        return Result.success(indexDataService.productList(vo, keyword));
    }

    @ApiOperation("后台首页数据分类列表")
    @PostMapping("/classList")
    @HttpApiMethod(apiKey = "admin.index.classList")
    public Result classList(MainVo vo, String keyword) {
        return Result.success(indexDataService.classList(vo, keyword));
    }

    @ApiOperation("后台首页数据材质列表")
    @PostMapping("/materialList")
    @HttpApiMethod(apiKey = "admin.index.materialList")
    public Result materialList(MaterialVo vo, String keyword) {
        return Result.success(materialService.getMaterialList(vo, keyword));
    }


}
